/**
 * @module file文件操作
 */


export default async function remove(file_url: string, productid: string, file_id: string[]) {
	const body = JSON.stringify({
		delfile_name: file_id,
		productid
	});
	try {
		const ret = await fetch(`${file_url}/delfile`, {
			body,
			credentials: 'include',
			headers: {
				'Content-Type': 'application/json'
			},
			method: 'POST'
		});
		const type = ret.headers.get('Content-Type');
		const status = ret.status;
		if (status > 0 && status < 300) {
			if (/json/i.test(type || '')) {
				return await ret.json();
			} else {
				return await ret.text();
			}
		} else {
			const data = await ret.text();
			if (/json/i.test(type || '')) {
				const err_msg = JSON.parse(data) as { msg: string, detail: string };
				if (err_msg.msg) {
					console.error(err_msg);
					throw new Error(err_msg.msg);
				} else {
					throw new Error(data);
				}
			} else {
				throw new Error(data);
			}
		}
	} catch (error) {
		console.error(error);
		return false;
	}
}
